package com.lzb.party.controller;

import com.lzb.party.controller.dto.AdminDto;
import com.lzb.party.entity.Admin;
import com.lzb.party.mapper.AdminMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired(required=false)
    private AdminMapper adminMapper;

    @GetMapping
    //查询所有数据
    public List<Admin> findAll(){
        return adminMapper.findAll();
    }

    @DeleteMapping("/{user}")
    public Integer delete(@PathVariable Integer user) {
        return adminMapper.deleteById(user);
    }

    @GetMapping("/page")
    //分页查询数据
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String user){
        pageNum = (pageNum-1)*pageSize;
        user = "%"+user+"%";
        List<Admin> data = adminMapper.selectPage(pageNum,pageSize,user);
        Integer total = adminMapper.selectTotal(user);
        Map<String, Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }

    @PostMapping("/login")
    public boolean login(@RequestBody AdminDto adminDto){
        //@RequestBody注释把前台的Json数据变成Java对象
        String user = adminDto.getUser();
        String pass = adminDto.getPass();
        if (StringUtils.isBlank(user)||StringUtils.isBlank(pass)){
            return false;
        }
        return adminMapper.login(user,pass).equals(1);
    }
}
